import requests
from lxml import etree

# 请求头
headers = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"
}

# 发起请求
response = requests.get(url="https://movie.douban.com/chart", headers=headers)

print(response)
# print(response.text)

# 将html文本转换对象
html = etree.HTML(response.text)
print(html)

# 使用xpath解析数据
# 获取所有的电影所在的标签
items = html.xpath("//tr[@class='item']")

# 循环解析每一个电影信息
for item in items:
    # 电影评分
    score = item.xpath("td/div/div/span[@class='rating_nums']/text()")
    score = "".join(score)

    # 电影名
    # replace:替换字符串中的字符
    name = item.xpath("td/div/a/text()")[0].replace("/", "").strip()

    # 描述
    info = item.xpath("td/div/p/text()")[0]

    print(name, score, info)
